"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 工具gs_cgroup
Case Name   : gs_cgroup -B参数是否存在缓冲区溢出问题
Create At   : 2022/12/15 20:05
Owner       : x00454388
Description :
    1. omm用户下执行gs_cgroup -u -B {backendname} -b 50
Expect      :
     1. gs_cgroup -B参数不存在缓冲区溢出问题
History     :
"""


import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'----{os.path.basename(__file__)} start----')
        self.userNode = Node('PrimaryDbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.b = 'A' * 100000

    def test_security(self):

        excute_cmd1 = f'source {self.DB_ENV_PATH};gs_cgroup --help'
        return_code = self.userNode.sh(excute_cmd1).return_code()
        self.logger.info('------return code值为0,当前连接为omm用户;'
                         'return code值为非0,当前连接为非omm用户------')
        self.logger.info("return code: " + str(return_code))
        self.assertTrue(return_code == 0)

        excute_cmd2 = f'source {self.DB_ENV_PATH};' \
                      f'gs_cgroup -u -B {self.b} -b 50'
        msg = self.userNode.sh(excute_cmd2).result()
        self.logger.info(msg)
        self.assertTrue(msg.find('Segmentation fault') == -1)

    def tearDown(self):
        self.logger.info(f'----{os.path.basename(__file__)} end----')
